package com.mxpio.webapp.controller;

import com.mxpio.webapp.entity.MonitorServer;
import com.mxpio.webapp.service.IMonitorServerService;
import com.mxpio.webapp.vo.MonitorServerVo;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 主机资源管理
 *
 * @author: Cheng
 * @date: 2022/10/28
 */
@Slf4j
@Tag(name = "MonitorServerController", description = "主机资源管理")
@RestController()
@RequestMapping("/monitor-server")
public class MonitorServerController {

    @Autowired
    private IMonitorServerService monitorServerService;

    @GetMapping("/list")
    @Operation(summary = "主机资源管理", description = "根据过滤字段filter获取主机列表", method = "GET")
    public Result<Page<MonitorServer>> list(Criteria criteria,
                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo) throws Exception {
        Pageable pageAble = PageRequest.of(pageNo - 1, pageSize);
        Page<MonitorServer> page = monitorServerService.pageServer(criteria, pageAble);
        return Result.OK(page);

    }

    @GetMapping("/status")
    @Operation(summary = "服务器性能监控", description = "根据过滤字段filter获取服务器性能列表", method = "GET")
    public Result<Page<MonitorServerVo>> serverStatus(Criteria criteria,
                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo) throws Exception {
        Pageable pageAble = PageRequest.of(pageNo - 1, pageSize);
        Page<MonitorServerVo> page = monitorServerService.queryServerStatus(criteria, pageAble);
        return Result.OK(page);

    }

    @PostMapping("/add")
    @Operation(summary = "添加主机资源管理", description = "添加主机资源管理", method = "POST")
    public Result<MonitorServer> add(@RequestBody MonitorServer monitorServer) throws Exception {
        monitorServerService.create(monitorServer);
        return Result.OK("添加主机资源管理成功",monitorServer);
    }

    @PutMapping("/edit")
    @Operation(summary = "更新主机资源管理", description = "更新主机资源管理", method = "PUT")
    public Result<MonitorServer> edit(@RequestBody MonitorServer monitorServer) throws Exception {
        monitorServerService.editMonitorServer(monitorServer);
        return Result.OK("编辑成功",null);
    }

    @DeleteMapping("/delete/{ids}")
    @Operation(summary = "删除主机资源管理", description = "删除主机资源管理", method = "DELETE")
    public Result<MonitorServer> delete(@PathVariable(name = "ids", required = true) String ids) throws Exception {
        String[] idList = ids.split(",");
        monitorServerService.delete(Arrays.asList(idList));
        return Result.OK("删除主机资源管理",null);
    }



}
